package com.wswy.wzcx.widget

import android.content.Context
import android.graphics.PointF
import android.support.v4.view.ViewConfigurationCompat
import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.widget.FrameLayout

class CardContainerLayout : FrameLayout {

    companion object {
        private const val TAG = "CardContainerLayout"
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {



        setOnTouchListener { v, event ->


            mGestureDetector.onTouchEvent(event)

            if(event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL){

                reset()
            }


            true
        }


    }


    private val scaledEdgeSlop = ViewConfiguration.get(context).scaledEdgeSlop

    private var pointDown = PointF()


    private val mGestureDetector by lazy {
        GestureDetector(context,object : GestureDetector.SimpleOnGestureListener(){
            override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {

                onMove(distanceX)

                return super.onScroll(e1, e2, distanceX, distanceY)
            }

            override fun onDown(e: MotionEvent?): Boolean {

                e?.run {
                    pointDown.set(x,y)
                }

                return super.onDown(e)
            }

            override fun onSingleTapUp(e: MotionEvent?): Boolean {
               reset()

                return super.onSingleTapUp(e)
            }

        })
    }

    private fun reset(){
        moveX = 0f
        pointDown.set(0f,0f)
    }


    private var moveX :Float= 0f

    private fun onMove(d:Float){
        moveX+=d

        Log.e(TAG, " --> 滑动距离 $moveX  当前方向${if (d > 0) "←左" else "右→"} ");

    }

}